export default{
    namespaced: true,
    state: {
        checked:[]
    },
    mutations: {
        SETCHECKED(state,payload){
            console.log(payload);
            let icd = state.checked.find(item=>item.id===payload.id)
            // console.log(icd);
            if(!icd){
                let obj ={
                    title:payload.title,
                    price:payload.price,
                    count:1,
                    id:payload.id
                }
                state.checked.push(obj)
            }
            else{
                icd.count++
            }
        },
        DELATE_CHECKED(state,payload,index){
            // console.log(1);
            // console.log(payload);
            payload.count--
            // let aaa = state.checked.find(item=>item.id===payload.id)
            // console.log(aaa);
            console.log(index);
            if(payload.count===0){
                state.checked.splice(index,1)
            }
            // console.log(this.$refs.aaa);
            // if(payload.count===0){
            // }
        }
    },
    actions: {
        GETARR(context,payload){
            context.commit('SETCHECKED',payload)
            // console.log(payload);
            context.commit('DELARR',payload,{root:true})
        },
        SUBTRACT(context,payload,index){
            context.commit('DELATE_CHECKED',payload,index)
            context.commit('DELATE_ARR',payload,{root:true})
        }
    },
    getters: {
        total(state){
            return (state.checked.reduce((total,item)=>{
                return total += item.count*item.price
            },0)).toFixed(2)
        }
    }
}